#ifndef _I_SERVLET_H
#define _I_SERVLET_H
#include "IPlugin.h"
#include <functional>
/**
 * @brief commuincation with virutal logic
 * Client(ISession) <--> Server(ISession)  
 * request(msgA)
 *             ->onMessage(msgA)
 *                             -> session->reply(msgB)
 *                                                   ->onMessage(msgB)
 */
class IServlet;
class ISession : public IBean {
public:
    virtual void reply(const void *data,int len) = 0;
    virtual void close() = 0;
    
    virtual IPlugin *getBusiness(const char *name) {
        return NULL;
    };
    template<typename T>
    T* Business() {
        return (T*) getBusiness(T::__businessName__);
    }
    virtual IServlet *getServlet() {
        return NULL;
    }
};
enum SERVLET_STATUS {
    STREAM = 0,
    CLOSE = 1,
    OPEN = 2,
    ERROR = -1,
};
class IServlet : public IPlugin {
public:
    virtual void onMessage(ISession *session,void* content,int len) = 0;
    virtual void onState(SERVLET_STATUS status,ISession *session,int code){}
    virtual void request(const void *data,int len) = 0;
    virtual void start() = 0;
    virtual void wait() = 0;
    virtual void stop() = 0;
    virtual void on(SERVLET_STATUS type,std::function<int(ISession *,void*,int)> handler) {}
    virtual ~IServlet(){}
};
#endif // _I_SERVLET_H
